//
//  SearchViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/29.
//  Copyright © 2015年 lushan. All rights reserved.
//

#import "SearchViewController.h"
#import "DiaryTableViewCell.h"
#import "MJRefresh.h"
#import "LSAppTools.h"
#import "DiaryModel.h"
#import "TravelDiaryDetailViewController.h"
@interface SearchViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property(nonatomic,strong)UISearchBar *mySearchBar;
@property(nonatomic,strong)UISearchController *searchController;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, assign) BOOL isUpLoad;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, copy) NSString *searchString;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    self.mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0 ,WIDTH, 44 * HEI)];
    self.mySearchBar.delegate = self;
    self.navigationItem.titleView = self.mySearchBar;
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 66 * HEI) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[DiaryTableViewCell class] forCellReuseIdentifier:@"DiaryTableViewCell"];
    
    
    [self addHeaderRefresh];
    [self addFooterRefresh];
    

    
    
    
    self.array = [NSMutableArray array];


    
}


-(void)addHeaderRefresh
{
    
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    self.myTableView.header = [MJRefreshHeader headerWithRefreshingBlock:^{
        NSLog(@"上拉");
        self.isUpLoad = NO;
        if (self == 0) {
            self.page = 1;
        }
        // 重新请求数据
        [self getAFNetWorkingData];
        
    }];
}

-(void)addFooterRefresh
{
    
    self.myTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        
        NSLog(@"上拉");
        self.isUpLoad = YES;
        self.page++;
        // 重新请求数据
        [self getAFNetWorkingData];
    }];
    
}




- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    //    self.navigationItem.hidesBackButton = NO;
    
    
    self.searchString = [self.mySearchBar text];
    self.myTableView.frame = CGRectMake(0, 50 * HEI, WIDTH, HEIGHT - 66 * HEI);
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 1; i < 52; i++) {
        NSString *string = [NSString stringWithFormat:@"a18d7359gw1ewdbb5kj49g20qo02fmyr-2－%d.tiff", i];
        UIImage *image = [UIImage imageNamed:string];
        [array addObject:image];
    }

    //小僵尸动画
    UIImageView *fashView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50 * HEI)];
    [self.view addSubview:fashView1];
    //设置播放图片数组
    fashView1.animationImages = array;
    //设置播放时长
    fashView1.animationDuration = 2;
    fashView1.animationRepeatCount = 0;
    //开始播放
    [fashView1 startAnimating];
    //僵尸移动
    [UIView animateWithDuration:10 animations:^{
        //动画结束的坐标
        fashView1.frame = CGRectMake(0 , 0, WIDTH, 50 * HEI);
        
    }];

    NSString *url_string = [NSString stringWithFormat:@"http://chanyouji.com/api/search/attractions.json?q=%@&page=1",self.searchString];
    [LSAppTools getDataWithURL:url_string blcok:^(id result) {
        [self.array removeAllObjects];
        for (NSMutableDictionary *dic in result) {
            DiaryModel *diaryModel = [[DiaryModel alloc] init];
            [diaryModel setValuesForKeysWithDictionary:dic];
            
            [self.array addObject:diaryModel];
            
        }
        
        fashView1.frame = CGRectMake(0, 0, 0, 0);
        self.myTableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT - 66 * HEI);
        [self.myTableView reloadData];
        

        
    }];

    
    [searchBar resignFirstResponder];

    
}

- (void)getAFNetWorkingData{
    if (self.isUpLoad == NO) {
        
        NSString *url_string = [NSString stringWithFormat:@"http://chanyouji.com/api/search/attractions.json?q=%@&page=1",self.searchString];
        [LSAppTools getDataWithURL:url_string blcok:^(id result) {
            [self.array removeAllObjects];
            for (NSMutableDictionary *dic in result) {
                DiaryModel *diaryModel = [[DiaryModel alloc] init];
                [diaryModel setValuesForKeysWithDictionary:dic];
                
                [self.array addObject:diaryModel];
                
            }
            [self.myTableView reloadData];
            
            
        }];
    }else{
        NSString *url_string = [NSString stringWithFormat:@"http://chanyouji.com/api/search/attractions.json?q=%@&page=%ld",self.searchString, self.page];
        [LSAppTools getDataWithURL:url_string blcok:^(id result) {
            for (NSMutableDictionary *dic in result) {
                DiaryModel *diaryModel = [[DiaryModel alloc] init];
                [diaryModel setValuesForKeysWithDictionary:dic];
                [self.array addObject:diaryModel];
                
            }
            
            
        }];
        
    }
    [self.myTableView.header endRefreshing];
    [self.myTableView.footer endRefreshing];
    [self.myTableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (self.array.count == 0) {
//        return 1;
//    }else{
        return self.array.count;
//    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DiaryTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

//    if (self.array.count ==0) {
//        cell.textLabel.text = @"无搜索内容";
//    }else{
        DiaryModel *diaryModel = [self.array objectAtIndex:indexPath.row];
        cell.diaryModel = diaryModel;
//    }
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (self.array.count == 0) {
//        return 10;
//    }else{
        return 100 * HEI;
//    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if (self.array.count != 0) {
        TravelDiaryDetailViewController *diaryDetailVC = [[TravelDiaryDetailViewController alloc] init];
        DiaryModel *diaryModel = [self.array objectAtIndex:indexPath.row];
        diaryDetailVC.diaryModel = diaryModel;
        
        
        
        [self.navigationController pushViewController:diaryDetailVC animated:YES];
//    }
    
    
}
-(void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
